package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.reggie.common.R;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;


    /**
     * 添加地址
     * @param addressBook
     * @return
     */
    @PostMapping
    public R saveAddress(@RequestBody AddressBook addressBook){

        Long userId = UserHolder.getUserId();

        addressBook.setUserId(userId);

        boolean save = addressBookService.save(addressBook);

        return save? R.success("添加地址成"):R.error("添加地址失败");
    }


    /**
     * 设置默认的地址
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    public R setDefaultAddress(@RequestBody AddressBook addressBook){

        //将所有的地址都设置为非默认地址
        LambdaUpdateWrapper<AddressBook> qw = new LambdaUpdateWrapper<>();
        qw.set(AddressBook::getIsDefault,0);
        qw.eq(AddressBook::getUserId,UserHolder.getUserId());
        addressBookService.update(qw);

        //将前端传来的地址设为默认地址
        addressBook.setIsDefault(1);
        boolean b = addressBookService.updateById(addressBook);

        return b? R.success("修改成功"): R.error("修改失败");
    }


    /**
     * 通过id查询地址
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R getById(@PathVariable Long id){

        AddressBook addressBook = addressBookService.getById(id);

        return R.success(addressBook);
    }


    /**
     * 查询用户的默认的地址
     * @return
     */
    @GetMapping("/default")
    public R getDefault(){

        LambdaUpdateWrapper<AddressBook> qw = new LambdaUpdateWrapper<>();
        qw.eq(AddressBook::getUserId,UserHolder.getUserId());
        qw.eq(AddressBook::getIsDefault,1);
        AddressBook one = addressBookService.getOne(qw);

        return R.success(one);
    }


    /**
     * 查询用户的所有地址
     * @return
     */
    @GetMapping("/list")
    public R getAllAddress(){

        LambdaQueryWrapper<AddressBook> qw = new LambdaQueryWrapper<>();
        qw.eq(AddressBook::getUserId,UserHolder.getUserId());
        List<AddressBook> list = addressBookService.list(qw);
        return R.success(list);

    }

    /**
     * 删除地址
     * @param ids
     * @return
     */
    @DeleteMapping
    public R  deleteAddress(@RequestParam("ids") List<Long> ids){

        boolean b = addressBookService.removeByIds(ids);

        return b? R.success("删除成功"): R.error("删除失败");
    }

    @PutMapping
    public R updateAddress(@RequestBody AddressBook addressBook){

        boolean b = addressBookService.updateById(addressBook);

        return b? R.success("修改成功"): R.error("修改失败");
    }
}
